package com.ikangtai.shecare.knowledge;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.BaseActivity;
import com.ikangtai.shecare.common.baseView.ProgressWebView;
import com.ikangtai.shecare.common.baseView.ScrollInterface;
import com.ikangtai.shecare.common.baseView.TopBar;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.InternetUtil;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class BannerActivity extends BaseActivity {
    private boolean isWebViewScrolledToTop = true;
    private TopBar topBar;
    private PtrClassicFrameLayout ptrFrame;
    private ProgressWebView webview;
    private ViewStub viewStub;
    private Button refresh_btn;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        initView();
        showBannerContent();
    }

    private void initView() {
        topBar = (TopBar) findViewById(R.id.topBar);
        topBar.setOnTopBarClickListener(new TopBar.OnTopBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void midLeftClick() {

            }

            @Override
            public void midRightClick() {

            }

            @Override
            public void rightClick() {

            }
        });

        Intent intent = getIntent();
        url = intent.getStringExtra("url");

        ptrFrame = (PtrClassicFrameLayout) findViewById(R.id.store_house_ptr_frame);
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (InternetUtil.hasInternet())
                    showWebBanner();
                else {
                    Toast.makeText(BannerActivity.this, R.string.network_anomalies, Toast.LENGTH_SHORT).show();
                    frame.refreshComplete();
                }
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                if(isWebViewScrolledToTop)
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                else
                    return false;
            }
        });

        webview = (ProgressWebView) findViewById(R.id.webview);
        viewStub = (ViewStub) findViewById(R.id.viewStub);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                ptrFrame.refreshComplete();
            }
        });
    }

    private void showBannerContent() {
        if (InternetUtil.hasInternet())
            showWebBanner();
        else
            showBadNetwork();
    }

    private void showWebBanner(){
        viewStub.setVisibility(View.GONE);
        webview.setVisibility(View.VISIBLE);
        loadData();

        webview.setOnCustomScroolChangeListener(new ScrollInterface() {
            @Override
            public void onSChanged(int l, int t, int oldl, int oldt) {
                AppLog.i("webview.getScrollY() = " + webview.getScrollY());
                //已经处于顶端
                if (webview.getScrollY() == 0) {
                    isWebViewScrolledToTop = true;
                } else
                    isWebViewScrolledToTop = false;
            }

        });
    }

    private void loadData(){
        webview.loadUrl("http://www.ikangtai.com/" + url + ".html");
    }

    private void showBadNetwork() {
        webview.setVisibility(View.GONE);
        viewStub.setVisibility(View.VISIBLE);

        refresh_btn = (Button) findViewById(R.id.refresh_btn);
        refresh_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (InternetUtil.hasInternet())
                    showWebBanner();
            }
        });
    }
}
